package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appWxMenuConfigDao")
public class AppWxMenuConfigDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppWxMenuConfig queryWithKeys(String id) throws SQLException {
		AppWxMenuConfig appWxMenuConfig = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  wx_menu_define WHERE wx_menu_id=?");
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				appWxMenuConfig = new AppWxMenuConfig();
								appWxMenuConfig.setWx_menu_id(rs.getString("wx_menu_id"));
								appWxMenuConfig.setWx_menu_name(rs.getString("wx_menu_name"));
								appWxMenuConfig.setApp_id(rs.getString("app_id"));
								appWxMenuConfig.setWx_menu_type(rs.getString("wx_menu_type"));
								appWxMenuConfig.setParent_menu_id(rs.getString("parent_menu_id"));
								appWxMenuConfig.setWx_menu_rank(Integer.parseInt(rs.getString("wx_menu_rank")));
								appWxMenuConfig.setWx_resp_actionid(rs.getString("wx_resp_actionid"));
								appWxMenuConfig.setWx_menu_url(rs.getString("wx_menu_url"));
								appWxMenuConfig.setWx_event_key(rs.getString("wx_event_key"));
							}
		} catch (SQLException e) {
			throw new SQLException("query wx_menu_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appWxMenuConfig;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppWxMenuConfig appWxMenuConfig) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into wx_menu_define(wx_menu_id  , app_id  , wx_menu_type  , wx_menu_name , wx_menu_url  , parent_menu_id  , wx_menu_rank  , wx_resp_actionid   , wx_event_key  ) values(? , ? , ? , ? , ? , ? , ? , ? , ? )";
			ps = conn.prepareStatement(sql);
						ps.setString(1, appWxMenuConfig.getWx_menu_id());
                        			ps.setString(2, appWxMenuConfig.getApp_id());
                        			ps.setString(3, appWxMenuConfig.getWx_menu_type());
                        			ps.setString(4, appWxMenuConfig.getWx_menu_name());
                        			ps.setString(5, appWxMenuConfig.getWx_menu_url());
                        			ps.setString(6, appWxMenuConfig.getParent_menu_id());
                        			ps.setInt(7, appWxMenuConfig.getWx_menu_rank());
                        			ps.setString(8, appWxMenuConfig.getWx_resp_actionid());
                        			ps.setString(9, appWxMenuConfig.getWx_event_key());
                        			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert wx_menu_define is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppWxMenuConfig appWxMenuConfig) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			
			
			String sql = "update wx_menu_define set wx_event_key=? , app_id=? , wx_menu_type=? , wx_menu_name=? , wx_menu_url=? , parent_menu_id=? , wx_menu_rank=? , wx_resp_actionid=? where wx_menu_id=?";
			ps = conn.prepareStatement(sql);
									ps.setString(1, appWxMenuConfig.getWx_event_key());
                        			ps.setString(2, appWxMenuConfig.getApp_id());
                        			ps.setString(3, appWxMenuConfig.getWx_menu_type());
                        			ps.setString(4, appWxMenuConfig.getWx_menu_name());
                        			ps.setString(5, appWxMenuConfig.getWx_menu_url());
                        			ps.setString(6, appWxMenuConfig.getParent_menu_id());
                        			ps.setInt(7, appWxMenuConfig.getWx_menu_rank());
                        			ps.setString(8, appWxMenuConfig.getWx_resp_actionid());
                        			ps.setString(9, appWxMenuConfig.getWx_menu_id());
                        			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update wx_menu_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from wx_menu_define where wx_menu_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete wx_menu_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
